# Copyright (c) 2025, yuxinyong and contributors
# For license information, please see license.txt

import frappe
from frappe.model.document import Document
from frappe.model.naming import make_autoname
from zelin_shoe_biz.utils import extract_last_number


class ShoeConstruction(Document):
	def autoname(self):
		"""
		RC-xxxxyy, xxxx是四位last_number, yy是流水号
		"""

		last_number = extract_last_number(self.last)
		self.name = make_autoname(f"RC-{last_number}.##")

	@frappe.whitelist()	
	def create_item(self, field):
		if not self.last: return

		last_number = extract_last_number(self.last)
		prefix = f"R{field[0].upper()}-"
		item_code = make_autoname(f"{prefix}{last_number}.##")
		doc_dict = {
			"doctype": "Item",
			"item_group": field.upper(),
			"last": self.last,
			"item_code": item_code
		}
		item_doc = frappe.get_doc(doc_dict).insert(ignore_permissions=1)
		self.set(field, item_doc.name)

		return item_doc.name
